package leetcode.array;

import java.util.Arrays;

/**
 * @author mazouri
 * @create 2021-05-02 22:53
 * <p>
 * 用双指针输出结果
 */
public class TwoSum_doublePointer {
    public static int[] twoSum(int[] nums, int target) {
        Arrays.sort(nums);
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int sum = nums[left] + nums[right];
            if (sum == target) {
                return new int[]{nums[left], nums[right]};
            } else if (sum > target) {
                right--;
            } else {
                left--;
            }
        }

        return new int[2];
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 5, 15};
        System.out.println(Arrays.toString(twoSum(nums, 7)));
    }
}
